﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Configuration;
using Ivy.FeedbackModule.DataAccess;
using Ivy.UserRegistration.Entity;
using Ivy.FeedbackModule.Entity;
using Ivy.FeedbackModule.BusinessEntity;
using Ivy.UserRegistration.DataAccess;
using Ivy.Common.Framework.EmailUtility;
using Ivy.Common.Framework.GeneralUtility;
using Ivy.FeedbackModule.IBusinessLogic;


namespace Ivy.FeedbackModule.BusinessLogic
{
    public class FeedbackModuleBus : IFeedbackModuleBus
    {
        #region Variables & Constants

        #endregion Variables & Constants

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Public Properties
        #endregion Public Properties

        #region Public Methods
        #region IFeedbackModuleBus Members
        public bool SubmitFeedBack(FeedbackUser feedbackUserEntity, Feedback feedbackEntity)
        {
            bool IsOperationSuccessful = false;

            FeedbackUserDao lFeedbackUserDao = new FeedbackUserDao();
            FeedbackDao lFeedbackDao = new FeedbackDao();
            FeedbackUser lFeedbackUser = lFeedbackUserDao.GetByEmail(feedbackUserEntity.FeedbackUserEmail);

            if (lFeedbackUser == null)
            {
                feedbackUserEntity.RegisteredUserkey = GetUserKey(feedbackUserEntity.FeedbackUserEmail);
                lFeedbackUserDao.Insert(feedbackUserEntity);
                lFeedbackUser = lFeedbackUserDao.GetByEmail(feedbackUserEntity.FeedbackUserEmail);
                feedbackEntity.FeedbackUserKey = lFeedbackUser.FeedbackUserKey;
            }
            else
            {
                feedbackEntity.FeedbackUserKey = lFeedbackUser.FeedbackUserKey;
            }
            lFeedbackDao.Insert(feedbackEntity);
            IsOperationSuccessful = true;
            string lFeedbackText = StringUtility.ConvertHtmlToString(feedbackEntity.FeedbackText);
            SendFeedbackNotificationEmail(lFeedbackUser.FeedbackUserName, lFeedbackUser.FeedbackUserEmail, lFeedbackText);


            return IsOperationSuccessful;
        }

        /// method for creating New FeedbackUser
        /// by ARMV
        ///
        public bool CreateFeedBackUser(FeedbackUser FeedbackUser)
        {
            bool IsOperationSuccessful = false;

            FeedbackUserDao lFeedbackUserDao = new FeedbackUserDao();
            lFeedbackUserDao.Insert(FeedbackUser);
            IsOperationSuccessful = true;

            return IsOperationSuccessful;
        }

        public bool IsFeedbackUserEmailExisting(FeedbackUser FeedbackUser)
        {
            bool IsFeedbackUserEmailExisting;

            FeedbackUserDao lFeedbackUserDao = new FeedbackUserDao();
            FeedbackUser lFeedbackUser;
            lFeedbackUser = lFeedbackUserDao.GetByEmail(FeedbackUser.FeedbackUserEmail);
            if (lFeedbackUser == null)
            {
                IsFeedbackUserEmailExisting = false;
            }
            else
            {
                IsFeedbackUserEmailExisting = true;
                FeedbackUser.FeedbackUserKey = lFeedbackUser.FeedbackUserKey;
            }

            return IsFeedbackUserEmailExisting;
        }

        /// method for Checking Existing FeedbackUser
        /// by ARMV
        ///
        public bool IsFeedbackUserExisting(string UserEmailID)
        {
            bool FeedbackUserExisting = true;

            FeedbackUserDao lFeedbackUserDao = new FeedbackUserDao();
            FeedbackUser lFeedbackUser;
            lFeedbackUser = lFeedbackUserDao.GetByEmail(UserEmailID);
            if (lFeedbackUser == null)
            {
                FeedbackUserExisting = false;
            }

            return FeedbackUserExisting;
        }

        public int GetUserKey(string EmailID)
        {
            int Userkey = -1;
            UserDao lUserDao = new UserDao();

            UserCriteria lUserCriteria = new UserCriteria();
            lUserCriteria.UserPrimaryEmailID = EmailID;
            List<User> lListUsers = new List<User>();

            lListUsers = lUserDao.GetByCriteria(lUserCriteria);
            if (lListUsers.Count > 0)
            {
                Userkey = lListUsers[0].UserKey;
            }
            else
            {
                Userkey = 1;
            }

            return Userkey;
        }

        public bool SendFeedbackNotificationEmail(string feedbackUserName, string feedbackUserEmailId, string feedbackText)
        {
            bool IsOperationSuccessful = false;

            EmailUtility lEmailUtility = new EmailUtility();
            MailMessage lMailMsg = new MailMessage();
            lMailMsg.Subject = "Dasideen - Feedback received notification";
            lMailMsg.IsBodyHtml = true;
            lMailMsg.Body = @"<html xmlns=""http://www.w3.org/1999/xhtml"" xml:lang=""en"">";
            lMailMsg.Body = lMailMsg.Body + @"<head> <style type=""text/css""> .style1 { color: #006600; } .style2 { text-align: left; font-size: small; } .style3 { font-size: large; } .style5 { font-size: large; font-weight: bold; color: #006600; } .style6 { text-align: left; font-size: small; color: #006600; } </style> </head>";
            lMailMsg.Body = lMailMsg.Body + @"<body style=""font-family: 'MS Sans Serif'"">";
            lMailMsg.Body = lMailMsg.Body + @"<table class=""style3"">";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td> <h1 class=""style1""> Feedback  Notification! </h1> </td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style3""> Dasideen has received a Feedback at the web site.</td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style3""> <span class=""style3"">User Name : </span><span class=""style5"">" + feedbackUserName + "</span></td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style3""> User Email : <span class=""style5"">" + feedbackUserEmailId + "</span></td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td> Feedback Details:</td> </tr>";

            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style6"">" + feedbackText + "</td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td> &nbsp; </td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Thanks and Regards </td> </tr> <tr> <td class=""style2""> Dasideen(The Ideas) Administrator -  Notification System </td> </tr> <tr> <td class=""style2""> (A Crowd Community) </td> </tr> <tr> <td class=""style2""> &nbsp; </td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Copyright &copy; 2012&nbsp; <a href=""http://www.dasideen.com/"">www.dasideen.com</a> </td></tr>";
            lMailMsg.Body = lMailMsg.Body + @"</table></body></html>";

            //lEmailUtility.AddEmailSenderAddressToMailMessage(lMailMsg, "frameworkivy@gmail.com", "Administrator FrameworkIvy");
            //lEmailUtility.AddEmailRecepientOrReplyAddressesToMailMessage(lMailMsg, EmailAddressType.SendTo, "frameworkivy@googlegroups.com");

            lEmailUtility.AddEmailSenderAddressToMailMessage(lMailMsg);
            lEmailUtility.AddEmailSenderAddressToMailMessage(lMailMsg);
            lEmailUtility.AddEmailRecepientOrReplyAddressesToMailMessage(lMailMsg, EmailAddressType.ReplyTo, lEmailUtility.GetComaSeperatedDefaultAlertMailAddresses());

            //lEmailUtility.AddEmailRecepientForAdminGroupToMailMessage(lMailMsg);

            lEmailUtility.SendEmail(lMailMsg);

            return IsOperationSuccessful;
        }

        public List<Feedback> GetAllFeedback()
        {
            var lFeedbackList = new List<Feedback>();

            FeedbackDao IFeedbackDao = new FeedbackDao();
            lFeedbackList = IFeedbackDao.GetAll();

            return lFeedbackList;
        }

        public List<FeedbackUser> GetAllFeedbackUser()
        {
            var lFeedbackUserList = new List<FeedbackUser>();

            FeedbackUserDao lFeedbackUserDao = new FeedbackUserDao();
            lFeedbackUserList = lFeedbackUserDao.GetAll();

            return lFeedbackUserList;
        }

        public List<Feedbacks> GetAllUserFeedback()
        {
            List<Feedbacks> lFeedbacksList = new List<Feedbacks>();

            FeedbackDao lFeedbackDao = new FeedbackDao();
            lFeedbacksList = lFeedbackDao.GetAllUserFeedback();

            return lFeedbacksList;
        }

        #endregion
        #endregion Public Methods

        #region Events
        #endregion Events

        #region Private Methods
        #endregion Private Methods
    }
}
